const shipfilter = {
  namespaced: true,
  state: {
    shipTypeList: [
      { label: "客船", code: "01" },
      { label: "普通货船", code: "02" },
      { label: "液货船", code: "03" },
      { label: "工程船", code: "04" },
      { label: "工作船", code: "05" },
      { label: "拖船", code: "06" },
      { label: "其他", code: "09" },
    ],
    shipTypeFilter: undefined,
    shipLengthList: [
      { label: "0-40", code: "1" },
      { label: "40-80", code: "2" },
      { label: "80-120", code: "3" },
      { label: "120-160", code: "4" },
      { label: "160-240", code: "5" },
      { label: "240-320", code: "6" },
      { label: ">320", code: "7" },
    ],
    // shipLengthFilter: {
    //   custom: false,
    //   customMin: undefined,
    //   customMax: undefined,
    //   lengths: ["1","2"],
    // },
    shipLengthFilter: undefined,
    shipStatusList: [
      { label: "在航", code: "0" },
      { label: "靠泊", code: "2" },
      { label: "锚泊", code: "1" },
    ],
    shipStatusFilter: undefined,
  },

  mutations: {
    SET_SHIPTYPE_FILTER(state, data) {
      state.shipTypeFilter = data;
    },
    SET_SHIPLENGTH_FILTER(state, data) {
      state.shipLengthFilter = data;
    },
    SET_SHIPSTATUS_FILTER(state, data) {
      state.shipStatusFilter = data;
    },
  },
  actions: {
    setShipTypeFilter({ commit }, data) {
      commit("SET_SHIPTYPE_FILTER", data);
      window.$cookie.set("_ship_filter_ship_types", JSON.stringify(data));
    },
    setShipLengthFilter({ commit }, data) {
      if (data.custom) {
        data = {
          custom: data.custom,
          customMin: data.customMin,
          customMax: data.customMax,
        };
      } else {
        data = {
          custom: data.custom,
          lengths: data.lengths,
        };
      }
      commit("SET_SHIPLENGTH_FILTER", data);
      window.$cookie.set("_ship_filter_ship_lengths", JSON.stringify(data));
    },
    setShipStatusFilter({ commit }, data) {
      commit("SET_SHIPSTATUS_FILTER", data);
      window.$cookie.set("_ship_filter_ship_status", JSON.stringify(data));
    },
    getShipTypeFilter({ state, commit }) {
      if (!state.shipTypeFilter) {
        let filter = window.$cookie.get("_ship_filter_ship_types");
        if (!filter) {
          const typeCodes = state.shipTypeList.map((val) => {
            return val.code;
          });
          filter = typeCodes;

          window.$cookie.set("_ship_filter_ship_types", JSON.stringify(filter));
        } else {
          filter = JSON.parse(filter);
        }
        commit("SET_SHIPTYPE_FILTER", filter);
      }

      return state.shipTypeFilter;
    },
    getShipStatusFilter({ state, commit }) {
      if (!state.shipStatusFilter) {
        let filter = window.$cookie.get("_ship_filter_ship_status");
        if (!filter) {
          const statusCodes = state.shipStatusList.map((val) => {
            return val.code;
          });
          filter = statusCodes;

          window.$cookie.set(
            "_ship_filter_ship_status",
            JSON.stringify(filter)
          );
        } else {
          filter = JSON.parse(filter);
        }
        commit("SET_SHIPSTATUS_FILTER", filter);
      }

      return state.shipStatusFilter;
    },

    getShipLengthFilter({ state, commit }) {
      if (!state.shipLengthFilter) {
        let filter = window.$cookie.get("_ship_filter_ship_lengths");
        if (!filter) {
          const lengthCodes = state.shipLengthList.map((val) => {
            return val.code;
          });
          filter = {
            custom: false,
            lengths: lengthCodes,
          };

          window.$cookie.set(
            "_ship_filter_ship_lengths",
            JSON.stringify(filter)
          );
        } else {
          filter = JSON.parse(filter);
        }
        commit("SET_SHIPLENGTH_FILTER", filter);
      }

      return state.shipLengthFilter;
    },
  },
};

export default shipfilter;
